package cn.aitrox.ry.service.config.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.config.api.LesionApi;
import cn.aitrox.ry.service.config.dto.LesionDto;
import cn.aitrox.ry.service.config.dto.in.LesionCreateInDto;
import cn.aitrox.ry.service.config.dto.in.LesionEditInDto;
import cn.aitrox.ry.service.config.dto.in.LesionGroupSearchInDto;
import cn.aitrox.ry.service.config.dto.in.LesionSearchInDto;
import cn.aitrox.ry.service.config.entity.LesionEntity;
import cn.aitrox.ry.service.config.service.LesionService;
import cn.aitrox.ry.util.ArrayUtil;
import cn.aitrox.ry.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class LesionController implements LesionApi {

    @Autowired
    private LesionService lesionService;

    @PostMapping(value = "/config/lesion/group/search")
    @Override
    public CommonPageInfo<LesionDto> lesionGroupSearch(@RequestBody LesionGroupSearchInDto input) {
        return lesionService.lesionGroupSearch(input.getPage(), input.getPageSize(), input.getName(), input.getValueList());
    }

    @PostMapping(value = "/config/lesion/search")
    @Override
    public CommonPageInfo<LesionDto> lesionSearch(@RequestBody LesionSearchInDto input) {
        PageInfo<LesionEntity> pageInfo = lesionService.search(input.getPage(), input.getPageSize(), input.getName(), input.getValueList());
        if (ArrayUtil.isEmpty(pageInfo.getList())) {
            return new CommonPageInfo(0, 0, null);
        }
        List<LesionDto> list = pageInfo.getList().stream().map(x -> {
            LesionDto lesion = new LesionDto();
            BeanUtil.copy(x, lesion);
            return lesion;
        }).collect(Collectors.toList());
        return new CommonPageInfo(pageInfo.getTotal(), pageInfo.getPages(), list);
    }

    @PostMapping(value = "/config/lesion")
    @Override
    public RespResult create(@Validated @RequestBody LesionCreateInDto input) {
        // TODO 校验参数是否有重复
        return lesionService.create(input);
    }


    @PutMapping(value = "/config/lesion")
    @Override
    public RespResult edit(@Validated @RequestBody LesionEditInDto input) {
        return lesionService.edit(input);
    }

    @GetMapping(value = "/config/lesion/maxCode")
    @Override
    public String maxCode() {
        return lesionService.maxValue();
    }
}
